package edu.zju.gis.dbfg.model;

import edu.zju.gis.dbfg.model.exception.ModelFailedException;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Hu
 * @date 2020/1/2
 **/
public abstract class BaseArgs implements Serializable {

  private final static Logger logger = LoggerFactory.getLogger(BaseArgs.class);

  public static <T extends BaseArgs> T initArgs(String args[], Class<T> clz) {
    try {
      Constructor con = clz.getConstructor();
      T mArgs = (T) con.newInstance();
      CmdLineParser parser = new CmdLineParser(mArgs);
      parser.parseArgument(args);
      return mArgs;
    } catch (NoSuchMethodException | CmdLineException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
      logger.error(e.getLocalizedMessage());
      throw new ModelFailedException(BaseArgs.class, "initArgs()", "init arguments failed", args);
    }
  }

    // TODO
    protected String getArgMsg() {
        return "";
    }

}
